package account;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;

/**
 * Kuemmert sich um die gesamte grafische Oberflaeche des Programmes.
 * 
 * @author Patrick Bauer
 * @version 1.0
 */
public class MainWindow extends JFrame {

    /**
     * Instanziert das Hauptfenster des Programms und befuellt es mit Bedienelementen.
     * 
     * @param accountCount die Anzahl der anzulegenden Konten
     */
    public MainWindow(int accountCount) {
        JSplitPane topdown = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
        JSplitPane leftright = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
        
        JPanel accountContainer = new JPanel(new BorderLayout(10,10));
        GridLayout gridLayout = new GridLayout((int) Math.ceil(accountCount / 5.0), 5);
        gridLayout.setVgap(20);
        JPanel accounts = new JPanel(gridLayout);
       
        accountContainer.add(new JPanel(), BorderLayout.WEST);
        accountContainer.add(new JPanel(), BorderLayout.EAST);
        accountContainer.add(new JPanel(), BorderLayout.NORTH);
        accountContainer.add(new JPanel(), BorderLayout.SOUTH);
        accountContainer.add(accounts, BorderLayout.CENTER);
        
        leftright.setPreferredSize(new Dimension(1,300));
        
        AccountModel model = new AccountModel();
        AccountController controller = new AccountController(model);
        
        AccountListView listView = new AccountListView();
        AccountChartView pieView = new AccountChartView();

        for(int i = 0; i < accountCount; i++) {
            model.addAccount();
            
            AccountActionView accountActionView = new AccountActionView(i, model);
            accountActionView.addRaiseListener(controller);
            accountActionView.addLowerListener(controller);
            
            model.addObserver(accountActionView);
            accounts.add(accountActionView);
        }
        
        model.addObserver(listView);
        model.addObserver(pieView);
        
        leftright.setLeftComponent(new JScrollPane(listView));
        leftright.setRightComponent(pieView);
        
        topdown.setTopComponent(leftright);
        topdown.setBottomComponent(accountContainer);
        
        this.add(topdown);
    }

    /**
     * Setzt die Parameter fuer die Anzeige des Fensters und zeigt es anschliessend an.
     *
     **/
    public void showWindow() {
        this.setTitle("Konten von Patrick Bauer");
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.pack();
        this.setVisible(true);
       
        JSplitPane topdown = (JSplitPane) this.getContentPane().getComponent(0);
        JSplitPane leftright = (JSplitPane) topdown.getTopComponent();
        leftright.setDividerLocation(0.5);
        
        Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
        this.setLocation((dim.width-this.getSize().width)/2, (dim.height-this.getSize().height)/2);
    }
    
}
